package zhangcy.day0109.qingyoutravel.dao.impl;


import zhangcy.day0109.qingyoutravel.dao.TourRouteDao;
import zhangcy.day0109.qingyoutravel.po.TourRoute;
import zhangcy.day0109.qingyoutravel.utils.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class TourRouteDaoImpl extends BaseDao implements TourRouteDao {
    @Override
    public List<TourRoute> queryAllTourRoutes() {
        List<TourRoute> tourRoutes = new ArrayList<>();
        TourRoute tourRoute = null;
        StringBuffer sql = new StringBuffer();
        sql.append(" SELECT TR_ID, TR_NAME, TR_TYPE, TR_PRICE, TR_PHONE, TR_USER, TO_CHAR(CREATE_TIME, 'yyyy-MM-dd') CREATE_TIME FROM TOUR_ROUTE ");
        rs = super.executeQuery(sql.toString());
        try {
            while (rs.next()) {
                tourRoute = new TourRoute(rs.getInt("TR_ID"), rs.getString("TR_NAME"),
                            rs.getInt("TR_TYPE"), rs.getDouble("TR_PRICE"),
                            rs.getString("TR_PHONE"), rs.getString("TR_USER"),
                            rs.getString("CREATE_TIME"));
                tourRoutes.add(tourRoute);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            super.closeAll(rs, pstmt, conn);
        }
        return tourRoutes;
    }

    @Override
    public int insert(TourRoute tourRoute) {
        int addRes = 0;
        StringBuffer sql = new StringBuffer();
        sql.append(" INSERT INTO TOUR_ROUTE(TR_ID, TR_NAME, TR_TYPE, TR_PRICE, TR_PHONE, TR_USER, CREATE_TIME)");
        sql.append(" VALUES(SEQ_TOUR_ROUTE_IDS.NEXTVAL, ?, ?, ?, ?, ?, TO_DATE(?,'yyyy-MM-dd'))");
        addRes = super.executeUpdate(sql.toString(), new Object[]{tourRoute.getTrName(), tourRoute.getTrType(),
                tourRoute.getTrPrice(), tourRoute.getTrPhone(), tourRoute.getTrUser(), tourRoute.getCreateTime()});
        return addRes;
    }

    @Override
    public int delete(String trName) {
        StringBuffer sql = new StringBuffer();
        sql.append(" DELETE FROM TOUR_ROUTE WHERE TR_NAME = ? ");
        return super.executeUpdate(sql.toString(), new Object[]{ trName });
    }
}
